package com.ruoyi.vote.app.controller;

import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.redis.RedisUtils;
import com.ruoyi.vote.admin.domain.vo.VoteActivityVo;
import com.ruoyi.vote.admin.domain.vo.VoteSignVo;
import com.ruoyi.vote.admin.service.IVoteActivityService;
import com.ruoyi.vote.admin.service.IVoteSignService;
import com.ruoyi.vote.app.domain.request.VoteActivityReq;
import com.ruoyi.vote.app.domain.request.VoteSignReq;
import com.ruoyi.vote.common.constants.ExamineConstants;
import com.ruoyi.vote.admin.notice.VoteNoticeJob;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.time.Duration;

/**
 * API - 活动相关
 * @author 立勤
 * @date 2023/07/17 15:46:24
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/saIgnore/activity")
public class ApiActivityController extends BaseController {

    private final IVoteActivityService activityService;
    private final IVoteSignService signService;

    /**
     * 活动查询
     * @param req       请求参数
     * @param pageQuery 分页参数
     * @return
     */
    @GetMapping("/query-activity-list")
    public TableDataInfo<VoteActivityVo> queryActivityList(VoteActivityReq req, PageQuery pageQuery){
        if (ObjectUtil.isNull(req.getVoteStatus())){
            throw new RuntimeException("请设置活动状态");
        }
        return activityService.queryPageList(req, pageQuery);
    }

    /**
     * 获取活动详情
     * @param aid
     * @return
     */
    @GetMapping("/get-activity/{aid}")
    public R<VoteActivityVo> getActivity(@PathVariable @NotNull(message = "活动不能为空") Long aid){
        // 热度+1
        activityService.updateWelcomeOne(aid);
        return R.ok(activityService.queryById(aid));
    }

    /**
     * 获取参赛选手集合
     * @param req       查询条件
     * @param pageQuery  分页参数
     */
    @PostMapping("/get-sign-list")
    public TableDataInfo<VoteSignVo> getSignList(@RequestBody VoteSignReq req, PageQuery pageQuery){
        if (ObjectUtil.isNull(req.getAId())){
            throw new RuntimeException("请选择活动");
        }
        req.setStatus(ExamineConstants.SUCCESS);
        return signService.queryPageList(req, pageQuery);
    }

    /**
     * 获取活动的选手详情
     * @param id   参赛ID
     */
    @GetMapping("/get-sign/{id}")
    public R<VoteSignVo> getSign(@PathVariable Long id){
        VoteSignVo voteSignVo = signService.queryById(id);
        voteSignVo.setStatus(ExamineConstants.SUCCESS);
        if(ObjectUtil.isNotNull(voteSignVo)){
            // 热度+1
            activityService.updateWelcomeOne(voteSignVo.getAId());
            signService.updateWelcomeOne(voteSignVo.getAId(),voteSignVo.getMId());
        }
        return R.ok(voteSignVo);
    }
}
